package com.wisdom.benefit.jufa.utils;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.content.Intent;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.text.TextUtils;
import android.util.Log;

@SuppressLint("NewApi")
public class NotificationMonitorService extends NotificationListenerService {
    public static String NOTIFICATION_PACKAGE_WECHAT = "com.tencent.mm";//微信
    public static String NOTIFICATION_PACKAGE_ALIPAYGPHONE = "com.eg.android.AlipayGphone";//支付宝

    // 在收到消息时触发
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        Bundle extras = sbn.getNotification().extras;
        // 获取接收消息APP的包名
        String notificationPkg = sbn.getPackageName();
        // 获取接收消息的抬头
        String notificationTitle = extras.getString(Notification.EXTRA_TITLE);
        // 获取接收消息的内容
        String notificationText = extras.getString(Notification.EXTRA_TEXT);
        if (NOTIFICATION_PACKAGE_WECHAT.equals(notificationPkg)) {//微信
            try {
                CreatNotify(notificationText);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (NOTIFICATION_PACKAGE_ALIPAYGPHONE.equals(notificationPkg)) {//支付宝
            try {
                CreatNotify(notificationText);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Log.i("XSL_Test", "Notification posted " + notificationTitle + " & " + notificationText);

    }

    // 在删除消息时触发
    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        // TODO Auto-generated method stub
        Bundle extras = sbn.getNotification().extras;
        // 获取接收消息APP的包名
        String notificationPkg = sbn.getPackageName();
        // 获取接收消息的抬头
        String notificationTitle = extras.getString(Notification.EXTRA_TITLE);
        // 获取接收消息的内容
        String notificationText = extras.getString(Notification.EXTRA_TEXT);
        Log.i("XSL_Test", "Notification removed " + notificationTitle + " & " + notificationText);
    }

    private void CreatNotify(String content) {

        //这里发送广播 去实现逻辑 静态注册的广播 android8.0以后 必须 用下面方法发送，否则接受不到
        if (!TextUtils.isEmpty(content)) {
            Intent intent = new Intent();
            intent.putExtra("key", content);
            intent.setAction("location.reportsucc");
            sendBroadcast(intent);
        }

    }
}

